/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  de.felixroske.jfxsupport.GUIState
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.GridPane
 *  javafx.stage.Modality
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.GysApplication;
import com.gys.common.business.GysUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.controller.BaseController;
import com.gys.controller.LoginController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.dataManger.TokenManger;
import com.gys.data.example.GetUserOutData;
import com.gys.view.LoginView;
import com.gys.view.StoreView;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class PhoneLoginController
extends BaseController
implements Initializable {
    @FXML
    private TextField code;
    @FXML
    private TextField phone;
    @FXML
    private GridPane login;
    @Autowired
    private LoginController loginController;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.clear();
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.login)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                GUIState.getStage().getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            PhoneLoginController.this.handleLoginAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
    }

    @FXML
    void accountLoginAction() {
        this.loginController.init();
        GysApplication.showView(LoginView.class);
    }

    @FXML
    void handleLoginAction() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        String phoneStr = this.phone.getText().trim();
        String codeStr = this.code.getText().trim();
        if (ObjectUtil.isEmpty(phoneStr) || ObjectUtil.isEmpty(codeStr)) {
            alert.setContentText("\u624b\u673a\u53f7\u4e0e\u9a8c\u8bc1\u7801\u4e0d\u80fd\u4e3a\u7a7a");
            alert.showAndWait();
            return;
        }
        HttpJson httpJson = GysUtil.LoginByPhone(phoneStr, codeStr);
        if (httpJson.isSuccess()) {
            GetUserOutData loginUser = (GetUserOutData)JSON.parseObject((String)httpJson.getMsg(), GetUserOutData.class);
            loginUser.setAccount(loginUser.getUserId());
            String token = loginUser.getToken();
            TokenManger.setToken(token);
            CacheUtil.put(token, loginUser);
            CacheUtil.put("storeList", loginUser.getStoreList());
            CacheUtil.put("logInUser", loginUser);
            GysApplication.showView(StoreView.class, (Modality)Modality.APPLICATION_MODAL);
        } else {
            alert.setContentText(httpJson.getMsg());
            alert.showAndWait();
        }
    }

    @FXML
    void sendCodeAction() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        String phoneStr = this.phone.getText().trim();
        if (ObjectUtil.isEmpty(phoneStr)) {
            alert.setContentText("\u8bf7\u8f93\u5165\u624b\u673a\u53f7");
            alert.showAndWait();
            return;
        }
        HttpJson httpJson = GysUtil.getCode(phoneStr);
        if (httpJson.isSuccess()) {
            alert.setContentText("\u9a8c\u8bc1\u7801\u5df2\u53d1\u9001");
        } else {
            alert.setContentText(httpJson.getMsg());
        }
        alert.showAndWait();
    }
}

